﻿


namespace BJYJ_Admin.Application.System.Services.User;


[ApiDescriptionSettings("System")]
public class SysDictService : IDynamicApiController, ITransient
{
    public readonly ISqlSugarClient _db;
    public SysDictService(ISqlSugarClient db)
    {
        _db = db;
    }

    /// <summary>
    ///  获取字典列表                      
    /// </summary>
    /// <returns></returns>
    [DisplayName("获取字典列表")]
    public virtual async Task<PageResult<SYS_DICT>> GetDictList([FromQuery] DictSearchDto input)
    {
        RefAsync<int> totalCount = 0;
        var user = await _db.Queryable<SYS_DICT>()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.DTYPE), x => x.DTYPE == input.DTYPE)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.DNAME), x => x.DNAME.Contains(input.DNAME))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.DVALUE), x => x.DVALUE.Contains(input.DNAME))
                    .WhereIF(input.STATUS.HasValue, x => x.STATUS == input.STATUS)
                    .OrderBy(x => x.DTYPE, OrderByType.Asc)
                    .OrderBy(x => x.ORDER_NUM, OrderByType.Asc)
                    .ToPageListAsync(input.page, input.limit, totalCount);
        return new PageResult<SYS_DICT>
        {
            page = input.page,
            limit = input.limit,
            totalcount = totalCount,
            data = user
        };
    }
    /// <summary>
    /// 新增字典    
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("新增字典")]
    public virtual async Task AddDict(DictInsertDto input)
    {
        var Dict = new SYS_DICT
        {
            DTYPE = input.DTYPE,
            DNAME = input.DNAME,
            DVALUE = input.DVALUE,
            STATUS = input.STATUS,
            CREATE_TIME = DateTime.Now,
            CREATE_BY = App.User?.FindFirstValue("ID")
        };
        await _db.Insertable(Dict).ExecuteCommandAsync();
    }
    /// <summary>
    /// 编辑字典
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("编辑字典")]
    public virtual async Task UpdateDict(SYS_DICT input)
    {
        input.UPDATE_TIME = DateTime.Now;
        input.UPDATE_BY = App.User?.FindFirstValue("ID");
        var result = await _db.Updateable(input)
                .IgnoreColumns(u => new { u.CREATE_BY, u.CREATE_TIME })
                .ExecuteCommandAsync();
        if (result == 0)
            throw Oops.Oh(ErrorCodeEnum.OP0002);
    }
    /// <summary>
    /// 删除字典
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("删除字典")]
    public virtual async Task DeleteDictByID(DictIDDto input)
    {
        var result = await _db.Deleteable<SYS_DICT>().Where(x => x.ID == input.ID).ExecuteCommandAsync();
        if (result == 0)
            throw Oops.Oh(ErrorCodeEnum.OP0003);
    }

    /// <summary>
    /// 字典详情
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("字典详情")]
    public virtual async Task<SYS_DICT> GeDictInfoByID(DictIDDto input)
    {
        var user = await _db.Queryable<SYS_DICT>().Where(x => x.ID == input.ID).FirstAsync();
        return user;
    }

    /// <summary>
    /// 启用禁用字典
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("启用禁用字典")]
    public virtual async Task UpdateStatusByID(DictStatusDto input)
    {
        var ID   = App.User?.FindFirstValue("ID");
        var result = await _db.Updateable<SYS_DICT>()
                                .SetColumns(it => it.STATUS == input.STATUS)
                                .SetColumns(it => it.UPDATE_TIME == DateTime.Now)
                                .SetColumns(it => it.UPDATE_BY == ID)
                                .Where(it => it.ID == input.ID)
                                .ExecuteCommandAsync();
        if (result == 0)
            throw Oops.Oh(ErrorCodeEnum.OP0000);
    }
}

